;MKEY - Give "mkey (filename)"

base equ 0
dmad1 equ base+80h
fcb equ base+5ch
fcr equ 32

stackp:: dw 0 ;keeps stack ptr
pasbuf:: ds 8

start:: ld (stackp),sp
 ld sp,stac0
 call open
 inc a
 jp z,nofdis
 xor a
 ld (fcb+fcr),a
 ld de,buf0
 call rdsec
 jp nz,nregds ;if eof
 ld de,buf0+80h
 call rdsec
 jp nz,nregds ;if eof
 call cpasgt ;gets coded keyword from old and new dmush
 call dmshmk ;reconstitutes dmush
 call comp ;ret nz if not correct
 jp nz,nregds
 call pasinv ;decode keyword from old and new nmush
 call chcvld ;ret c if invalid keyword
 jp c,nregds
 call pasdis ;display valid keyword
reb:: ld sp,(stackp)
 jp base

 ;sr open read fcb
open:: ld de,fcb
 ld c,0fh
 call base+5
 ret

 ;sr read (fcb) to (de), ret nz if eof
rdsec:: ld c,1ah
 call base+5 ;set dma
 ld de,fcb
 ld c,14h
 call base+5 ;read sequential
 or a
 ret ;nz if eof

 ;sr get coded keyword from old and new dmush
cpasgt:: ld c,51 ;arb start offset
 ld b,8 ;ct
clp:: ld hl,buf0+80h ;new dmush
 ld a,c
 add a,l
 ld l,a
 jr nc,clp2
 inc h ;start byte addr
clp2:: ld ix,buf0+50h ;new nmush adr
 ld a,b
 and 7
 ld e,a
 ld d,0
 add ix,de
 ld a,(hl)
 ld e,a
 and 0aah
 rra
 xor e
 dec h
 sub (hl)
 inc h
 ld (ix),a
 ld a,c
 add a,23 ;col incr
 and 7fh
 ld c,a
 djnz clp
 ret

 ;sr reconstitute mush
dmshmk:: ld c,51 ;arb start pt
 ld b,80h ;length
mlp:: ld hl,pag1+80h
 ld a,c
 add a,l
 ld l,a
 jr nc,mlp2
 inc h
mlp2:: ld ix,buf0+50h ;poss keyword
 ld a,b ;use b value for keyword ptr
 and 7
 ld e,a
 ld d,0
 add ix,de
 ld a,(ix)
 add a,(hl)
 ld e,a
 and 0aah
 rra
 xor e
 ld (hl),a
 ld a,c
 add a,23 ;col incr
 and 7fh
 ld c,a
 djnz mlp
 ret

 ;sr compare reconstituted mush
comp:: ld hl,pag1+80h
 ld de,buf0+80h
 ld b,80h
comp2:: ld a,(de)
 cp (hl)
 ret nz
 inc hl
 inc de
 djnz comp2
 ret

 ;sr decode keyword from old and new nmush
pasinv:: ld ix,pasbuf
 ld de,4 ;arb start offset
 ld b,8 ;ct
invlp:: ld hl,buf0+50h
 ld iy,pag1+50h
 add hl,de
 add iy,de
 ld a,(hl)
 ld c,a
 and 55h
 rla
 xor c
 sub (iy)
 ld (ix),a
 ld a,e
 add a,3 ;col incr
 and 7
 ld e,a
 inc ix
 djnz invlp
 ret

 ;sr ret c if invalid keyword char
chcvld:: ld hl,pasbuf
 ld b,8
vldlp:: ld a,(hl)
 cp ' '
 jr z,vldchr
 cp '/'
 ret c
 cp ':'
 jr c,vldchr
 cp '@'
 ret c
 cp '['
 ccf
 ret c
vldchr:: inc hl
 djnz vldlp
 xor a ;clear carry
 ret

nofdis:: ld de,nofmsg
 call string
 jp reb
nofmsg:: db 'No file',0dh,0ah,'$'

nregds:: ld de,nregms
 call string
 jp reb
nregms:: db 'File irregular or not locked'
 db 0dh,0ah,'$'

pasdis:: ld de,pasms1
 call string
 ld hl,pasbuf
 ld b,8
pdlp:: ld a,(hl)
 call disp
 inc hl
 djnz pdlp
 ld de,pasms2
 call string
 ret
pasms1:: db 0dh,0ah,'Keyword - $'
pasms2:: db 0dh,0ah,0dh,0ah,'Don''t '
 db 'forget it again!',0dh,0ah,'$'

string:: ld c,9
 call base+5
 ret

disp:: push bc
 push de
 push hl
 ld e,a
 ld c,2
 call base+5
 pop hl
 pop de
 pop bc
 ret

pag1:: .phase base+100h

 db 0dh,0ah,'==<Locked file>== ',0dh,0ah,1ah
  ;as a seq of instns, this gives a jump either to
  ;122h or to 173h

fill:: ds base+122h-fill ;foll instn shd be 122h
  ;173h will be set to jr 122h later

sendms:: ld hl,base+100h
 ld a,(hl)
 cp 1ah
 jp z,base
 push bc
 push de
 push hl
 ld e,a
 ld c,2
 call base+5
 pop hl
 pop de
 pop bc
 inc hl
 jr sendms+3

  ;next byte 13ah
 db 0f5h,79h,10h,0afh,23h,09h
 db 51h,00h,19h,71h,0d9h,6fh,0b8h,0b6h
 db 70h,26h,0a6h,63h,51h,03h,44h,20h

nmush:: db 57h,7eh,0c5h,14h,8fh,47h,0c1h,27h ;adr 150h
 db 0feh,0eeh,9fh,0edh,09h,0a9h,33h,76h
 db 0e2h,11h,72h,0d7h,0e3h,5dh,74h,66h
 db 66h,39h,0fh,2dh,15h,34h,0ch,81h
 db 25h,61h,33h,09h,12h,3eh,4eh,37h
 db 96h,0b0h,0ebh,41h,89h,0a0h,24h,78h

dmush:: db 48h,0e0h,23h,0afh,2ah,77h,0f6h,86h ;adr 180h
 db 0c3h,3ah,0d8h,7eh,5bh,7fh,0c1h,0eh
 db 57h,0f8h,30h,26h,11h,0fh,2eh,0d8h
 db 08h,6dh,0bah,7fh,8ch,0cch,90h,4ah

 db 95h,0a5h,0e4h,9fh,76h,5fh,0e0h,01h
 db 1fh,22h,9ah,77h,3ch,5dh,0a0h,0a7h
 db 75h,0a7h,0cfh,76h,0ach,40h,6fh,0aah
 db 3eh,79h,56h,5eh,69h,77h,3ah,64h

 db 3eh,56h,53h,4dh,01h,29h,47h,0b2h
 db 61h,85h,6ch,4ah,0a9h,0a2h,0d8h,0f3h
 db 9fh,4fh,0bah,32h,0c2h,43h,4dh,31h
 db 8ch,0ach,09h,58h,5ah,0fh,75h,0f7h

 db 0aah,73h,0a5h,9dh,0f3h,2dh,0beh,58h
 db 03h,4ah,0d9h,21h,30h,4eh,0d7h,75h
 db 0a8h,98h,82h,02h,41h,9ch,02h,0eh
 db 0c4h,61h,63h,61h,10h,0a2h,42h,48h

 .dephase

buf0:: ds 100h
 ds 20h ;for stack

stac0:: end start
